package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/infos")
    public ModelAndView infos(ModelMap model) throws Exception {
        List<Resume> resumeList = resumeService.findAll();
        model.addAttribute("list", resumeList);
        return new ModelAndView("infos");
    }

    @RequestMapping("/save")
    public String save(Resume resume) throws Exception {
        resumeService.save(resume);
        return "redirect:/resume/infos";
    }

    @RequestMapping("/delete")
    public String delete(Long resumeId) throws Exception {
        resumeService.delete(resumeId);
        return "redirect:/resume/infos";
    }

    @RequestMapping("/update")
    public ModelAndView update(ModelMap model, Long resumeId) {
        if (resumeId != null) {
            Resume resume = resumeService.findById(resumeId);
            model.addAttribute("resume", resume);
        }
        return new ModelAndView("update");
    }

    @RequestMapping("/add")
    public ModelAndView add() {
        return new ModelAndView("add");
    }
}
